from typing import List

import yaml
from pydantic import BaseModel
from pydantic.v1 import BaseSettings, validator


class UieConfig(BaseModel):
    schema_list: List[str]
    onnx_path: str
    clas_id: str
    text_system_path: str

    @validator('schema_list', pre=True)
    def validate_schema_list(cls, v):
        if isinstance(v, str):
            return v.split(',')
        return v


class PathConfig(BaseModel):
    clas_path: str
    text_image_path: str
    text_system_path: str
    uvdoc_path: str


class Settings(BaseSettings):
    uie_config: List[UieConfig]
    path_config: PathConfig


try:
    with open("config2.yaml", "r", encoding="utf-8") as file:
        config_data = yaml.safe_load(file)
    settings = Settings(uie_config=[UieConfig(**item) for item in config_data["uie_config"]],
                        path_config=PathConfig(**config_data["path_config"]))
except FileNotFoundError:
    print("配置文件未找到，请检查文件路径是否正确。")
except Exception as e:
    print(f"配置文件解析失败：{e}")
